﻿using KAKA.Domain;
using KAKA.Infrastructure;
using KAKA.Models;
using KAKA.Repository;
using Morus;
using Morus.Infrastructure;
using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib;

namespace KAKA.Controllers
{
    public class MemberController : BaseController
    {
        public MemberController()
            : base()
        {
            MemberRepository = IoC.Resolve<IMemberRepository>();
        }

        public IMemberRepository MemberRepository { get; private set; }

        public ActionResult Index(MemberIndexModel model)
        {
            var members = MemberRepository.GetAll();

            if (model == null || model.Members == null)
            {
                model = new MemberIndexModel
                {
                    Members = members.Select(r =>
                        new MemberModel
                        {
                            Id = r.Id.Shrink(),
                            Portrait = WebUtil.GetLargePortrait(r),
                            Nickname = r.Nickname,
                            Age = r.Age.HasValue ? r.Age.ToString() : string.Empty,
                            Declaration = r.Declaration.Content,
                            Occupation = (DictionaryRepository.FindByType("occupation").FirstOrDefault(q => q.Value == r.Occupation) ?? new Dictionary { Name = string.Empty }).Name,
                            Education = (DictionaryRepository.FindByType("education").FirstOrDefault(q => q.Value == r.Education) ?? new Dictionary { Name = string.Empty }).Name,
                            HasCrierion = r.Criterion != null,
                            AgeMax = r.Criterion != null ? r.Criterion.AgeMax.ToString() : string.Empty,
                            AgeMin = r.Criterion != null ? r.Criterion.AgeMin.ToString() : string.Empty,
                            HeightMax = r.Criterion != null ? r.Criterion.HeightMax.ToString() : string.Empty,
                            HeightMin = r.Criterion != null ? r.Criterion.HeightMin.ToString() : string.Empty,
                            HasPhotograph = r.Criterion != null ? r.Criterion.Photograph : false,
                            OnlyGrade = r.Criterion != null ? r.Criterion.Grade : false,
                            LimitCity = r.Criterion != null ? r.Criterion.City : string.Empty,
                            LimitProvince = r.Criterion != null ? r.Criterion.Province : string.Empty,
                        }).ToList()
                };
            }

            return View(model);
        }

        public ActionResult Criterion(SearchModel model)
        {
            if (IsCurrentUserAuthenticated && !model.Searched)
            {
                var member = MemberRepository.Get(CurrentUser.Id);
                if (member.Criterion != null)
                {
                    var criteria = member.Criterion;
                    model = new SearchModel()
                    {
                        AgeMin = criteria.AgeMin.ToString(),
                        AgeMax = criteria.AgeMax.ToString(),
                        HeightMin = criteria.HeightMin.ToString(),
                        HeightMax = criteria.HeightMax.ToString(),
                        Province = criteria.Province,
                        City = criteria.City,
                        Education = criteria.Education,
                        EducationOver = criteria.EducationOver,
                        Grade = criteria.Grade,
                        Photograph = criteria.Photograph,
                        Marry = criteria.Marry,
                        Searched = true,
                    };
                }
            }

            ViewBag.AgeMinSet = WebUtil.GetRange(18, 65, 0, true, "不限");
            ViewBag.AgeMaxSet = WebUtil.GetRange(18, 65, 0, true, "不限");
            ViewBag.HeightMinSet = WebUtil.GetRange(140, 230, 0, true, "不限");
            ViewBag.HeightMaxSet = WebUtil.GetRange(140, 230, 0, true, "不限");
            ViewBag.EducationSet = DictionaryRepository.FindByType("education").ToSelectList(true, "不限");
            ViewBag.MarrySet = DictionaryRepository.FindByType("marry").ToSelectList(true, "不限");

            return PartialView(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(SearchModel model)
        {
            var spec = Spec<Member>.Any();
            if (model.AgeMin.IsNotEmpty())
            {
                var ageMin = int.Parse(model.AgeMin);
                spec = spec.And(Spec<Member>.Eval(r => r.Age >= ageMin));
            }
            if (model.AgeMax.IsNotEmpty())
            {
                var ageMax = int.Parse(model.AgeMax);
                spec = spec.And(Spec<Member>.Eval(r => r.Age <= ageMax));
            }
            if (model.HeightMin.IsNotEmpty())
            {
                var heightMin = int.Parse(model.HeightMin);
                spec = spec.And(Spec<Member>.Eval(r => r.Height >= heightMin));
            }
            if (model.HeightMax.IsNotEmpty())
            {
                var heightMax = int.Parse(model.HeightMax);
                spec = spec.And(Spec<Member>.Eval(r => r.Height <= heightMax));
            }
            if (model.Marry.IsNotEmpty())
            {
                spec = spec.And(Spec<Member>.Eval(r => r.Marry == model.Marry));
            }
            if (model.Marry.IsNotEmpty())
            {
                spec = spec.And(Spec<Member>.Eval(r => r.Marry == model.Marry));
            }
            if (model.Province.IsNotEmpty())
            {
                spec = spec.And(Spec<Member>.Eval(r => r.Province == model.Province));
            }
            if (model.City.IsNotEmpty())
            {
                spec = spec.And(Spec<Member>.Eval(r => r.City == model.City));
            }
            if (model.Education.IsNotEmpty())
            {
                if (model.EducationOver)
                {
                    var education = int.Parse(model.Education);
                    spec = spec.And(Spec<Member>.Eval(r => r.EducationValue >= education));
                }
                else
                {
                    spec = spec.And(Spec<Member>.Eval(r => r.Education == model.Education));
                }
            }
            if (model.Photograph)
            {
                spec = spec.And(Spec<Member>.Eval(r => r.Portrait != null));
            }
            if (model.Grade)
            {
                spec = spec.And(Spec<Member>.Eval(r => (r.Grade | Grade.VIP1) == Grade.VIP1));
            }

            var members = MemberRepository.Find(spec);
            var viewModel = new MemberIndexModel
            {
                Members = members.ToList().Select(r =>
                    new MemberModel
                    {
                        Id = r.Id.Shrink(),
                        Portrait = WebUtil.GetLargePortrait(r),
                        Nickname = r.Nickname,
                        Age = r.Age.HasValue ? r.Age.ToString() : string.Empty,
                        Declaration = r.Declaration.Content,
                        Occupation = (DictionaryRepository.FindByType("occupation").FirstOrDefault(q => q.Value == r.Occupation) ?? new Dictionary { Name = string.Empty }).Name,
                        Education = (DictionaryRepository.FindByType("education").FirstOrDefault(q => q.Value == r.Education) ?? new Dictionary { Name = string.Empty }).Name,
                        HasCrierion = r.Criterion != null,
                        AgeMax = r.Criterion != null ? r.Criterion.AgeMax.ToString() : string.Empty,
                        AgeMin = r.Criterion != null ? r.Criterion.AgeMin.ToString() : string.Empty,
                        HeightMax = r.Criterion != null ? r.Criterion.HeightMax.ToString() : string.Empty,
                        HeightMin = r.Criterion != null ? r.Criterion.HeightMin.ToString() : string.Empty,
                        HasPhotograph = r.Criterion != null ? r.Criterion.Photograph : false,
                        OnlyGrade = r.Criterion != null ? r.Criterion.Grade : false,
                        LimitCity = r.Criterion != null ? r.Criterion.City : string.Empty,
                        LimitProvince = r.Criterion != null ? r.Criterion.Province : string.Empty,
                    }).ToList()
            };

            return Index(viewModel);
        }

        [Authorization]
        [MobileVerification]
        public ActionResult Detail(string id)
        {
            var marrySet = DictionaryRepository.FindByType("marry").ToSelectList();
            var childSet = DictionaryRepository.FindByType("child").ToSelectList();
            var educationSet = DictionaryRepository.FindByType("education").ToSelectList();
            var occupationSet = DictionaryRepository.FindByType("occupation").ToSelectList();
            var incomeSet = DictionaryRepository.FindByType("income").ToSelectList();
            var friendTypeSet = DictionaryRepository.FindByType("friendType").ToSelectList();
            var zodiacSet = DictionaryRepository.FindByType("zodiac").ToSelectList();
            var bloodSet = DictionaryRepository.FindByType("blood").ToSelectList();
            var asterismSet = DictionaryRepository.FindByType("asterism").ToSelectList();
            var nationSet = DictionaryRepository.FindByType("nation").ToSelectList();
            var religionSet = DictionaryRepository.FindByType("religion").ToSelectList();
            var nationalitySet = DictionaryRepository.FindByType("nationality").ToSelectList();
            var monotropicSet = DictionaryRepository.FindByType("monotropic").ToSelectList();
            var dinkSet = DictionaryRepository.FindByType("dink").ToSelectList();
            var singletonSet = DictionaryRepository.FindByType("singleton").ToSelectList();
            var livingSet = DictionaryRepository.FindByType("living").ToSelectList();
            var carSet = DictionaryRepository.FindByType("car").ToSelectList();
            var houseSet = DictionaryRepository.FindByType("house").ToSelectList();
            var healthSet = DictionaryRepository.FindByType("health").ToSelectList();
            var smokeSet = DictionaryRepository.FindByType("smoke").ToSelectList();
            var drinkSet = DictionaryRepository.FindByType("drink").ToSelectList();
            var lifeSet = DictionaryRepository.FindByType("life").ToSelectList();
            var exerciseSet = DictionaryRepository.FindByType("exercise").ToSelectList();

            var member = MemberRepository.Get(id.ToGuid());
            var model = new MemberModel()
            {
                Nickname = member.Nickname,
                Birthday = member.Birthday.HasValue ? member.Birthday.Value.ToString("yyyy-MM-dd") : string.Empty,
                Age = member.Age.HasValue ? member.Age.Value.ToString() : string.Empty,
                Marry = WebUtil.GetSelectListText(marrySet, member.Marry),
                Child = WebUtil.GetSelectListText(childSet, member.Child),
                Education = WebUtil.GetSelectListText(educationSet, member.Education),
                Province = member.Province,
                City = member.City,
                Occupation = WebUtil.GetSelectListText(occupationSet, member.Occupation),
                Income = WebUtil.GetSelectListText(incomeSet, member.Income),
                FriendType = WebUtil.GetSelectListText(friendTypeSet, member.FriendType),

                RegisteredProvince = member.RegisteredProvince,
                RegisteredCity = member.RegisteredCity,
                Height = member.Height.ToString(),
                Weight = member.Weight.HasValue ? member.Weight.Value.ToString("#.") : string.Empty,
                Zodiac = WebUtil.GetSelectListText(zodiacSet, member.Zodiac),
                Blood = WebUtil.GetSelectListText(bloodSet, member.Blood),
                Asterism = WebUtil.GetSelectListText(asterismSet, member.Asterism),
                Nation = WebUtil.GetSelectListText(nationSet, member.Nation),
                Religion = WebUtil.GetSelectListText(religionSet, member.Religion),
                Nationality = WebUtil.GetSelectListText(nationalitySet, member.Nationality),

                Monotropic = WebUtil.GetSelectListText(monotropicSet, member.Monotropic),
                Dink = WebUtil.GetSelectListText(dinkSet, member.Dink),
                Singleton = WebUtil.GetSelectListText(singletonSet, member.Singleton),
                Living = WebUtil.GetSelectListText(livingSet, member.Living),
                Car = WebUtil.GetSelectListText(carSet, member.Car),
                House = WebUtil.GetSelectListText(houseSet, member.House),

                Health = WebUtil.GetSelectListText(healthSet, member.Health),
                Smoke = WebUtil.GetSelectListText(smokeSet, member.Smoke),
                Drink = WebUtil.GetSelectListText(drinkSet, member.Drink),
                Life = WebUtil.GetSelectListText(lifeSet, member.Life),
                Exercise = WebUtil.GetSelectListText(exerciseSet, member.Exercise),

                Trade = member.Trade,
                TradeType = member.TradeType,
                WorkState = member.WorkState,
                IncomeDetail = member.IncomeDetail,
                Company = member.Company,
                Department = member.Department,
                Duty = member.Duty,
                Investment = member.Investment,
                School = member.School,
                Major = member.Major,

                Declaration = member.Declaration.Content,

                GenderValue = member.Gender == Gender.Female ? "女" : "男",
                Portrait = WebUtil.GetLargePortrait(member),
                Id = member.Id.Shrink(),

            };

            model.TotalPhotos = member.DefaultAlbum.Photographs.Count;
            model.Photos = member.DefaultAlbum.Photographs.Take(10).Select(r =>
                new PhotographModel
                {
                    Url = r.Url,
                    Caption = r.Caption,
                }).ToList();

            return View(model);
        }

        public ActionResult Album(string id)
        {
            return View();
        }

        public ActionResult Gallery(string id)
        {
            var member = MemberRepository.Get(id.ToGuid());
            var model = new GalleryModel
            {
                Id = member.Id.Shrink(),
                Nickname = member.Nickname
            };

            ViewBag.Photographs = member.DefaultAlbum.Photographs.ToList();

            return View(model);
        }

        public ActionResult Hello(string id)
        {
            return View();
        }

        public ActionResult Letter(string id, string message)
        {
            return View();
        }
    }
}
